var Query = RichBase.extend({
	statics : {
		currentPage : 1,
		last : "",
		queryState : {
			loading : '<li class="loading" style="text-align:center; line-height:1; height:400px;"><p style="color:#aeaeae; font-size:12px; padding-top:300px;">加载数据...</p><img src="images/other/loading_1.gif" alt="" /></li>',
			empty : '<li class="loading" style="text-align:center; line-height:1; height:400px;"><p style="color:#aeaeae; font-size:12px; padding-top:300px;">没有数据...</p></li>',
			fail : '<li class="loading" style="text-align:center; line-height:1; height:400px;"><p style="color:#aeaeae; font-size:12px; padding-top:300px;">请求失败...</p></li>',
			unlogin : '<li class="loading" style="text-align:center; line-height:1; height:400px;"><p style="color:#aeaeae; font-size:12px; padding-top:300px;">登录过期，请重新登录...</p></li>',
			timeout : '<li class="loading" style="text-align:center; line-height:1; height:400px;"><p style="color:#aeaeae; font-size:12px; padding-top:300px;">网络请求超时...</p></li>',
			serverError : '<li class="loading" style="text-align:center; line-height:1; height:400px;"><p style="color:#aeaeae; font-size:12px; padding-top:300px;">请求出错...</p></li>'
		},
		isObjEmpty : function(obj){
			for(var i in obj){
				return false;
			}
			return true;
		}
	}, 
	EVENTS : {
		"click" : {
			"#pagenavW .pageBtn" : "onPrevNextClick"
		}
	},
	init : function(opt){
		this.prevBtn = $("#prevPageBtn");
		this.nextBtn = $("#nextPageBtn");
		function getQueryString(name) {
			var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
			var r = window.location.search.substr(1).match(reg);
			if (r != null) return unescape(r[2]); return null;
		}
		$(".info_url").attr("href",'userinfo.html?id='+getQueryString("?"))
		$("#did").val(getQueryString("?"));
		$(".credit_active").attr("aid",getQueryString("?"))
		
	},
	onPrevNextClick : function(that,e){
		var tarBtn = $(e.currentTarget);
		if($("#prevPageBtn").attr("data-first")!=0){
			that.prevBtn.removeClass("disable");
		}else{
			that.prevBtn.addClass("disable");
		}
		if(tarBtn.hasClass("disable")) return false;
		var curPage = $(".page_num_").val();
		var toPage = tarBtn.hasClass("prevBtn") ? (curPage-1) : (curPage+1);
		var all = $(".nextBtn").attr("total");
		var aid = $(".credit_active").attr("aid");
		var mid = Number($("#prevPageBtn").attr("data-mid"));
		var last_ = Number($("#prevPageBtn").attr("data-last"));
		var first_ = Number($("#prevPageBtn").attr("data-first"));
		var total_ = Number($("#nextPageBtn").attr("total"));
		var title = $(".searchInp").val();
			if(tarBtn.hasClass("prevBtn")){
				that.nextBtn.removeClass("disable");
				if(first_=="0"){
					that.prevBtn.addClass("disable");
					return false;
				}
				else{
						qQuery.query({
							currentPage : first_,
							title:title
						})
				}

			}else{
				that.prevBtn.removeClass("disable");
				if(last_<=total_){
						qQuery.query({
							currentPage : last_,
							title:title
						})
				}else{
					that.nextBtn.addClass("disable");
				}
			}
			
	},
	query : function(opt){
		var that = this;
		var aid = opt.id; 
		var now = $(".search_titile").attr("now");
		var opt = opt || {};
		var pageSize = opt.pageSize || 1;
		var currentPage = opt.currentPage || "1";
		//var currentPage = opt.currentPage || "0";
		var sid = opt.sid;
		var pid = opt.pid;
		var tyo = opt.tyo || "1";
		var action = opt.action || "1";
		var dtype = opt.dtype || "4";
		var bt = opt.bt ;
		var pa = opt.pa || "0"; 
		var et = opt.et || now;
		var data_li = opt.data_li;
		var ttimeout = opt.ttimeout || 20 * 60 * 1000;
		var data_li = $(".pub_top_left").attr("data_li");
		var listUl = $("#mlistUl");
		var title = opt.title;
		var pagenavW = $("#pagenavW");
		var status = this.statics.queryState;
		var data = {
				currentPage : currentPage,
				title : title,
				action:3
			};
			PFT.Ajax({
				url : "module/pft_announce/announce.php",
				type : "GET",
				dataType : "json",
				data : data,
				loading : function(){
					listUl.html(status["loading"]);
					pagenavW.hide();
					},
				removeLoading : function(res){
					that.statics.last = "";
					that.statics.currentPage += 1;
					listUl.html("");
					pagenavW.show();
				}
			},function(res){
					if(res.status=="success"){
						that.buildHtml(res);
						$(".noticeBox").show();
					}
					else{
						PFT_GLOBAL.U.Alert("fail",'<p style="width:240px">抱歉无相关搜索结果</p>');
						$(".noticeBox").hide();
						$("#pagenavW").hide();
					}
					// if(!that.statics.isObjEmpty(lists)){ 
						// $(".tbCon").show();
						// //console.log(li_type)
						
					// }else{
						// listUl.html(status["empty"]);
						// $("#pagenavW").hide();
						// $(".tbCon").hide();
					// }
			})
	},
	buildHtml : function(res){
		var that = this;
		var lists = res.list;
		var str = "";
		var last = res.last;
		var data = lists;
		var total = res.total;
		var totalPage = res.totalPage || "0";
		var currentPage = res.currentPage;
		$(".nextBtn").attr("total",totalPage);
		$("#prevPageBtn").attr("data-mid",currentPage);
		$("#prevPageBtn").attr("data-last",currentPage+1);
		$("#prevPageBtn").attr("data-first",currentPage-1);
		$(".search_titile").attr("time",totalPage)
		for(var i in data){
			str +=   '<li class="list" onclick=\"javascript:window.open(\'pft_announce.html?id='+data[i]["id"]+'&m=con\')\">';
            str +=   '    <p class="tip"  onclick=\"javascript:window.open(\'pft_announce.html?id='+data[i]["id"]+'&m=con\')\">'+data[i]["title"]+'</p>';
            str +=   '    <div class="con" onclick=\"javascript:window.open(\'pft_announce.html?id='+data[i]["id"]+'&m=con\')\">'+data[i]["details"]+'</div>';
            str +=   '    <p class="bottomBox"> ';
            str +=   '        <span>'+data[i]["create_time"]+'</span>';
           // str +=   '        <span class="margin20">赞：'+data[i]["thumbs"]+'</span>';
            str +=   '        <span class="setWidth">评论：'+data[i]["remarks"]+'</span>';
            str +=   '    </p>';
            str +=   '</li>';
		}

		$(".noticeBox").html(str);
		$("html,body").animate({"scrollTop":0})
		$("#total_num").text("共"+totalPage+"页");	
		$("#whichPageNum").text(currentPage);
	}
});
var qQuery = new Query({container:$(".rtWrap")});
var search = RichBase.extend({
	statics : {},
	EVENTS : {
		"click" : {
			".searchBtn" : "search_sub", 
			".del_x" : "del",
			".page_sub" : "bl"
			}},
	init : function(opt){
		var that = this;
		qQuery.query({

		});
	},
	search_sub :function(){ 
		var title = $(".searchInp").val();
		if(!title){
			PFT_GLOBAL.U.Alert("fail",'<p style="width:240px">请输入搜索关键字</p>');
			return false
		}else{
				qQuery.query({
					action:3,
					title : title
				});
		}
	},
	del : function(){
		var title_ = $(".searchInp").val();
		if(!title_){
			$(".searchInp").val("");
			var title = $(".searchInp").val();
			qQuery.query({
				title : title
			});	
		}else{
			$(".searchInp").val("");
			var title = $(".searchInp").val();
			qQuery.query({
				title : title
			});	
		}
		
	},
	bl : function(){
		var page_size = Number($(".page_num_").val());
		if(!page_size || !PFT_GLOBAL.U.istiveNum(page_size)){
			PFT_GLOBAL.U.Alert("fail",'<p style="width:240px">抱歉页数输入有误</p>');
			$(".page_num_").val("")
			return false;
		}
		var all = Number($(".nextBtn").attr("total"));
		var bt = $("#startDate").val();
		var et = $("#endDate").val();
		var action = $(".noticeTab").attr("data");
		var title = $(".searchInp").val();
		var data_li = $(".pub_top_left").attr("data_li");
		var li_type = $(".noticeTab").attr("li_type");
		if(page_size<=all){
					qQuery.query({
						currentPage : page_size,
						title : title
					});
			
		}
		else{
			return false;
		}
		$(".page_num_").val("");
		
	}
});
new search({container:$(".rtWrap")});



